package com.shumi.rbac.service;

import com.shumi.enums.IReturnEnum;
import com.shumi.fun.Encryption;
import com.shumi.rbac.mapper.PubLoginMapper;
import com.shumi.rbac.model.PubLogin;
import com.shumi.rbac.model.User;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Transactional
@Service
public class AuthService {

    @Autowired
    PubLoginMapper loginMapper;
    @Autowired
    SystemService systemService;
    public ReturnEnum login(String logincode, String password) {
        User user = systemService.getUserByLoginName(logincode);
        if(user==null) return ReturnEnum.FAILED_2;
        if(!systemService.validatePassword(password,user.getPassword()))
            return ReturnEnum.FAILED_1;
//        PubLogin flag = loginMapper.selectByPrimaryKey(logincode);
//        if (flag == null) {
//            return ReturnEnum.FAILED_2;
//        }
//        String pwd=Encryption.EncryptionStr(password,Encryption.MD5);
//        if (!flag.getPassword().equals(pwd)) {
//            return ReturnEnum.FAILED_1;
//        }
        return ReturnEnum.SUCCESS_1;
    }

    public ReturnEnum reset(PubLogin login) {
        // TODO Auto-generated method stub
        PubLogin f = loginMapper.selectByPrimaryKey(login.getLogincode());
        if (f == null) {
            return ReturnEnum.FAILED_3;
        }
        String pwd=Encryption.EncryptionStr(login.getPassword(),Encryption.MD5);
        f.setPassword(pwd);
        loginMapper.updateByPrimaryKeySelective(f);
        return ReturnEnum.SUCCESS_2;
    }

    @Getter
    public static  enum  ReturnEnum implements IReturnEnum {
        SUCCESS_1(10,"登陆成功",true),
        FAILED_1(-11,"登陆失败,用户密码不匹配",false),
        FAILED_2(-12,"登陆失败,用户名不存在",false),
        SUCCESS_2(11,"密码重置成功",true),
        FAILED_3(-13,"密码重置失败,当前用户不存在",false),
        ;
        String code;
        String message;
        boolean success;
        Object returnObject;
        ReturnEnum(int code, String message, boolean success) {
            this.code=AuthService.class.getSimpleName()+"."+ code;
            this.message=message;
            this.success=success;
        }
    }
}
